package cc.wanforme.chipmunity.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cc.wanforme.chipmunity.common.bean.WebResponse;
import cc.wanforme.chipmunity.controller.base.BaseController;
import cc.wanforme.chipmunity.system.po.Reply;
import cc.wanforme.chipmunity.system.service.interfaces.ReplyService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @author wanne
 * 2019年8月17日
 * 
 */
@Controller
@RequestMapping("/reply")
public class ReplyController extends BaseController {
	private static final Logger log = LoggerFactory.getLogger(ReplyController.class);
	
	@Autowired
	private ReplyService replyService;
	
	@RequestMapping("/add")
	@ResponseBody
	@ApiOperation("添加新回复")
	@ApiImplicitParam(name="Reply", dataType="reply", value="添加新回复", required=true)
	@PreAuthorize("hasAuthority('reply:add')")
	public WebResponse addPassage(@RequestBody(required=true) Reply reply) {
		try {
			return replyService.saveReply(reply);
		} catch (Exception e) {
			log.error("添加新回复", e);
			return WebResponse.fail("回复失败！服务器异常！");
		}
	}
	
	@RequestMapping("/update")
	@ResponseBody
	@ApiOperation("更新回复")
	@ApiImplicitParam(name="Reply", dataType="reply", value="更新回复，"
			+ "不更新passageId，userId，number", required=true)
	@PreAuthorize("hasAuthority('reply:update')")
	public WebResponse updatePassage(@RequestBody(required=true) Reply reply) {
		try {
			return replyService.updateReply(reply);
		} catch (Exception e) {
			log.error("更新回复", e);
			return WebResponse.fail("回复失败！服务器异常！");
		}
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	@ApiOperation("删除回复")
	@ApiImplicitParam(name="id", dataType="Id", value="删除回复", required=true)
	@PreAuthorize("hasAuthority('reply:delete')")
	public WebResponse updatePassage(@RequestParam(required=true) Long id) {
		try {
			return replyService.deleteReply(id);
		} catch (Exception e) {
			log.error("删除回复", e);
			return WebResponse.fail("删除失败！服务器异常！");
		}
	}
}
